<script lang="ts" setup>
/* eslint-disable */
// eslint-disable-next-line vue/no-setup-props-destructure
import { ref, watch, reactive, computed } from "vue";
import unitForm from "../unit-form.vue";
const emit = defineEmits<{
  (e: "update:visible", val: boolean): void;
  (e: "save-success", data: any, res: any): void; //新增保存 修改保存 成功回调
}>();
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  unitId: {
    type: String
  },
  parentUnitId: {
    type: String
  }
});
const UnitFormRef = ref();
const visibleRefValue = ref(props.visible);
watch(
  () => props.visible,
  val => {
    visibleRefValue.value = val;
  }
);
watch(visibleRefValue, val => {
  emit("update:visible", val);
});

const UnitFormDialogState = reactive({
  dialogTitle: computed(() => (props.unitId ? "编辑部门" : "新增部门")),
  /** 关闭处理 */
  handleClose: () => {
    visibleRefValue.value = false;
  },

  /** 保存按钮点击事件 */
  onSaveButtonClick: () => {
    UnitFormRef.value.save();
  },

  /** 保存回调 */
  onSaveSuccess: (formData: any, res: any) => {
    emit("save-success", formData, res);
    visibleRefValue.value = false;
  }
});
</script>
<template>
  <div class="jxtech-dialog-form">
    <el-dialog
      v-model="visibleRefValue"
      :title="UnitFormDialogState.dialogTitle"
      width="80%"
      destroy-on-close
      :before-close="UnitFormDialogState.handleClose"
    >
      <unit-form
        ref="UnitFormRef"
        :unitId="unitId"
        :parentUnitId="parentUnitId"
        @save-success="UnitFormDialogState.onSaveSuccess"
      ></unit-form>
      <template #footer>
        <span class="dialog-footer">
          <jxtech-cancel-button @click="UnitFormDialogState.handleClose"></jxtech-cancel-button>
          <jxtech-ok-button @click="UnitFormDialogState.onSaveButtonClick"> </jxtech-ok-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
